package com.jcfk.system.controller;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import com.jcfk.system.domain.query.SysMessageTypeQuery;
import com.jcfk.system.domain.dto.SysMessageTypeDTO;
import com.jcfk.system.service.SysMessageTypeService;
import com.jcfk.common.core.utils.ExcelUtils;
import com.jcfk.system.domain.excel.SysMessageTypeExcel;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 消息类别管理
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2023-12-08
 */
@Api(tags = {"消息类别管理管理"})
@RestController
@RequestMapping("sysmessagetype")
public class SysMessageTypeController {
    @Autowired
    private SysMessageTypeService sysMessageTypeService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({@ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<SysMessageTypeQuery>", name = "requestParam", value = "", required = true)})
    @ApiOperation("分页查询消息类别管理")
    @PostMapping("/queryByPage")
    @ResponseBody
    public RetPage<SysMessageTypeDTO> page(@RequestBody ReqPageParam<SysMessageTypeQuery> requestParam) {
        DataPage<SysMessageTypeDTO> page = new DataPage((long) requestParam.getCurrent(), (long) requestParam.getSize(), requestParam.getSort().getOrderItem(), SysMessageTypeDTO.class);
        page = sysMessageTypeService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 消息类别管理
     * 根据id 查询详情
     */
    @ApiOperation("查询消息类别管理")
    @PostMapping("/get/{id}")
    @ResponseBody
    public RetResult<SysMessageTypeDTO> info(@PathVariable("id") String id) {
        SysMessageTypeDTO item = sysMessageTypeService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 消息类别管理
     * 根据id 查询详情
     */
    @ApiOperation("查询消息类别管理")
    @PostMapping("/getlist")
    @ResponseBody
    public RetResult<?> getlist(@RequestBody SysMessageTypeQuery query) {
        List<SysMessageTypeDTO> item = sysMessageTypeService.getList(query);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理消息类别管理
     */
    @ApiOperation("保存消息类别管理")
    @PostMapping("/save")
    @ResponseBody
    @RequestLogHeader(modular = "消息类别管理管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid SysMessageTypeDTO sysMessageType) {
        // 数据校验
        RetResult ret = sysMessageTypeService.isValid(sysMessageType);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(sysMessageType.getId())) {
            // 新增
            sysMessageTypeService.insert(sysMessageType);
        } else {
            // 更新
            sysMessageTypeService.update(sysMessageType);
        }
        return RetResult.ok();
    }

    /**
     * 批量删除消息类别管理
     */
    @ApiOperation("批量删除消息类别管理")
    @ResponseBody
    @PostMapping("/delete")
    @RequestLogHeader(modular = "消息类别管理管理", businessType = "批量删除")
    public RetResult<?> deleteList(@RequestBody SysMessageTypeDTO sysMessageType) {
        sysMessageTypeService.logicDeleteList(sysMessageType);
        return RetResult.ok();
    }


    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody ReqPageParam<SysMessageTypeQuery> requestParam, HttpServletResponse response) throws Exception {
        List<SysMessageTypeDTO> list = sysMessageTypeService.queryByPage(requestParam.getData());
        ExcelUtils.exportExcelToTarget(response, null, list, SysMessageTypeExcel.class);
    }
}
